﻿using FRMS.DAL;
using FRMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.BUS
{
    public class ElevatorBUS
    {
        public ResponseMessage AddElevator(ElevatorInfo elevatorInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorDAL da = new ElevatorDAL();
            int count = da.CheckElevator(elevatorInfo.BuildID, elevatorInfo.ElevatorName, elevatorInfo.ID);
            if (count > 0)
            {
                message.errcode = 1;
                message.errmsg = "电梯名字重复，新增失败";
            }
            else
            {
                int laddercount= da.CheckLadderID(elevatorInfo.LadderID, elevatorInfo.ID);
                if (laddercount > 0)
                {
                    message.errcode = 1;
                    message.errmsg = "电梯梯号重复，新增失败";
                }
                else
                {
                    bool result = da.AddElevator(elevatorInfo);
                    if (result)
                    {
                        message.errcode = 0;
                    }
                    else
                    {
                        message.errcode = 1;
                        message.errmsg = "新增电梯失败";
                    }
                }
               
            }


            return message;
        }

        public ResponseMessage ImportElevator(ElevatorInfo elevatorInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorDAL da = new ElevatorDAL();
            bool result = da.ImportElevator(elevatorInfo);
            if (result)
            {
                message.errcode = 0;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "导入电梯失败";
            }

            return message;
        }

        public ResponseMessage EditElevator(ElevatorInfo elevatorInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorDAL da = new ElevatorDAL();
            List<ElevatorInfo> lst = new List<ElevatorInfo>();
            lst = da.QueryElevatorInf(elevatorInfo.ID);
            if (lst.Count > 0)
            {
                if(lst[0].pFloorInfo!= elevatorInfo.pFloorInfo)
                {
                    VoucherDAL dq = new VoucherDAL();
                    VoucherBUS bq = new VoucherBUS();
                    ModbusRtu modbus = new ModbusRtu();
                    List<VoucherInfo> vou_list = dq.QueryListByDevinfo(elevatorInfo.ID, elevatorInfo.projectID);
                    for(int i=0;i< vou_list.Count; i++)
                    {
                        OwnerDAL ownerDAL = new OwnerDAL();
                        OwnerInfo ownerInfo = ownerDAL.LoadOwnerInfo(vou_list[i].OwnerID);
                        string phyfloor = bq.SortFloor(vou_list[i].AuthorizedFloor, vou_list[i].FloorInfo, elevatorInfo.pFloorInfo);
                        string AuthData = modbus.GetCallCmd(phyfloor, ownerInfo.UUID);
                        if(AuthData!= vou_list[i].AuthData)//授权数据不相等时的操作
                        {
                            bool res = dq.UpdateVoucherAuthData(vou_list[i].ID, AuthData);
                        }
                    }
                    
                    //查找对应的设备，然后根据显示楼层修改参数值

                }
            }
            int laddercount = da.CheckLadderID(elevatorInfo.LadderID, elevatorInfo.ID);
            if (laddercount > 0)
            {
                message.errcode = 1;
                message.errmsg = "电梯梯号已存在，修改失败";
            }
            else
            {

                bool result = da.UpdateElevator(elevatorInfo);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "修改电梯信息失败";
                }
            }


            return message;
        }

        public ResponseMessage DeleteElevator(ElevatorInfo elevatorInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorDAL da = new ElevatorDAL();
            if (da.CheckDevice(elevatorInfo))
            {
                bool result = da.DelElevator(elevatorInfo);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "删除电梯失败";
                }
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "该电梯下绑定设备，请先删除设备";
            }


            return message;
        }

        public ResponseMessage QueryElevator(ElevatorInfo elevatorInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorDAL da = new ElevatorDAL();
            message.errcode = 0;
            message.data = da.QueryElevatorInfo(elevatorInfo);
            message.total = da.QueryElevatorInfoCount(elevatorInfo);

            return message;
        }

        public ResponseMessage QueryElevatorList(ElevatorInfo elevatorInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorDAL da = new ElevatorDAL();
            message.errcode = 0;
            message.data = da.QueryElevatorList(elevatorInfo);
            message.total = da.QueryElevatorListCount(elevatorInfo);

            return message;
        }

        public ResponseMessage LoadElevator(ElevatorInfo elevatorInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorDAL da = new ElevatorDAL();
            message.errcode = 0;
            message.data = da.LoadElevator(elevatorInfo.ID);

            return message;
        }

        public ResponseMessage SwitchMode(ElevatorInfo elevatorInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorDAL da = new ElevatorDAL();
            ApplyJLBUS BUS = new ApplyJLBUS();
            try
            {
                BUS.LiftModeCMD(elevatorInfo);
                message.errcode = 0;
                message.errmsg = "设置成功";
                message.data = null;
                message.total = 0;
            }
            catch(Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "切换电梯工作模式";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "切换电梯工作模式异常";
                message.data = null;
                message.total = 0;
            }
            //return message;
            //等面板升级完成后再进行功能测试-等待面板下发命令到电梯控制器后，控制器返回到平台再来进行模式的UpdateMode
            da.UpdateMode(elevatorInfo);
            //message.errcode = 0;
            return message;
        }
    }
}
